<?php
/**
 * file:MissionExchange.php
 * This file is part of J-Game.
 *
 * @link    https://www.f4soft.com
 * @document https://www.f4soft.com
 * @contact 173120209@qq.com
 * @autho   :jackliang
 */
namespace F4soft\glass;

use F4soft\library\Database;

class MissionExchange {
    private static $instance;

    /**
     * @return \F4soft\glass\MissionExchange
     */
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     *执行加索引的操作
     */
    private function exchangeIndexTable() {
        Database::getInstance()->exec("ALTER TABLE `MissionOneModel` ADD INDEX index_name ( `avatarId` )");
    }

    private function getMissionOldData() {
        $data    = Database::getInstance()->select("MissionOneModel", ["avatarId"]);
        $avatars = [];
        foreach ($data as $k => $v) {
            array_push($avatars, $v['avatarId']);
        }
        echo "------datas---------------" . PHP_EOL;
        if (count($avatars) <= 0) {
            $result = Database::getInstance()->select("AvatarModel", ["id", "uid"], ["robotId" => 0]);
        } else {
            $result = Database::getInstance()->select("AvatarModel", ["id", "uid"], ["robotId" => 0, "uid[!]" => $avatars,]);
        }
        echo "------result---------------date=" . date("Y-m-d H:i:s") . PHP_EOL;
        $number = count($result);
        foreach ($result as $key => $avatar) {
            $time                       = time();
            $missionModels              = Database::getInstance()->select("MissionModel", "*", ["parentId" => $avatar['id']]);
            $missionTargetModels        = Database::getInstance()->select("MissionTargetModel", "*", ["parentId" => $avatar['id']]);
            $missonOneModel             = [];
            $missonOneModel['avatarId'] = $avatar['uid'];
            $missonOneModel['missions'] = json_encode($missionModels);
            $missonOneModel['targets']  = json_encode($missionTargetModels);
            $this->insertMissionNew($missonOneModel);
            $now = time();
            echo " insert entity-----use time" . ($now - $time) . "(还剩下待完成" . ($number--) . ")" . PHP_EOL;
        }
        echo "------all done------------date=" . date("Y-m-d H:i:s") . PHP_EOL;
    }

    private function insertMissionNew($data) {
        $count = Database::getInstance()->count("MissionOneModel", ["avatarId"], ["avatarId" => $data['avatarId']]);
        if ($count > 0) {
            echo " insert entity faiure avatarId exsit" . $data['avatarId'] . PHP_EOL;
            return;
        }
        Database::getInstance()->insert("MissionOneModel", $data);
    }

    public function main() {
        ini_set("memory_limit", "1024M");
        $this->exchangeIndexTable();
        $this->getMissionOldData();
    }

    private function moreMissonDone($data) {
        $arrint = array_chunk($data, 4, true);
        for ($i = 0; $i < 4; $i++) {
            $pid = pcntl_fork();
            if ($pid == -1) {
                die("could not fork");
            } elseif ($pid) {
                echo $pid;
                echo "I'm the Parent $i\n";
            } else {
                Database::getInstance()->insert("MissionOneModel", $arrint[$i]);
                echo "进程" . $i . "在执行:" . PHP_EOL;
                exit;
            }
        }
        // 等待子进程执行结
        while (pcntl_waitpid(0, $status) != -1) {
            $status = pcntl_wexitstatus($status);
            echo "Child $status completed\n";
        }
    }
}